package cn.easy.mortgage.utils;

import cn.easy.base.utils.Md5Util;
import cn.easy.mobile.utils.Constants;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TokenUtils {
    public static final Map<String, Object> TOKEN_MAP = Collections.synchronizedMap(new HashMap());

    public static String getToken(String key, String sessionId) {
        String token = Md5Util.md5Base64(key + sessionId + System.currentTimeMillis());

        Map varPool = new HashMap<String, Object>();
        varPool.put(Constants.ACCESS_TOKEN, token);
        varPool.put(Constants.ADD_TIME, new Date().getTime());
        TOKEN_MAP.put(sessionId, varPool);
        return token;
    }

    public static Boolean matchesToken(String key, String sessionId) {
        Map varPool = (Map) TOKEN_MAP.get(sessionId);
        if (varPool == null) {
            return false;
        }
        String token = String.valueOf(varPool.get(Constants.ACCESS_TOKEN));
        if (StringUtils.isNoneEmpty(key, token) && StringUtils.equals(key, token)) {
            return true;
        }
        return false;
    }
}
